package org.aaron.mp3player.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.aaron.mp3player.model.Mp3Info;

import android.os.Environment;

public class FileUtils {

	public static final int RESULT_SUCCESS = 0;
	public static final int RESULT_FAIL = -1;
	public static final int RESULT_FILE_IS_EXIST = 1;
	private String SDCradRoot;

	public String getSDCradRoot() {
		return SDCradRoot;
	}

	public FileUtils() {
		SDCradRoot = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
	}

	/**
	 * 在sd卡上创建目录 返回0创建成功 返回-1创建失败
	 * 
	 * @param pathname
	 * @return
	 */
	public int create2SdDir(String pathname) {
		File file = new File(SDCradRoot + pathname);
		if (file.exists()) {
			return FileUtils.RESULT_SUCCESS;
		}
		if (file.mkdir()) {
			return FileUtils.RESULT_SUCCESS;
		}
		return FileUtils.RESULT_FAIL;
	}

	/**
	 * 在sd卡上创建文件 返回0创建成功 返回-1创建失败
	 * 
	 * @param filename
	 * @return
	 * @throws IOException
	 */
	public int create2SdFile(String filename) throws IOException {
		File file = new File(SDCradRoot + filename);
		if (file.exists()) {
			file.deleteOnExit();
		}
		if (file.createNewFile()) {
			return FileUtils.RESULT_SUCCESS;
		}
		return FileUtils.RESULT_FAIL;
	}

	/**
	 * 在sd卡上文件是否存在 返回1已存在 返回-1文件不存在
	 * 
	 * @param filename
	 * @return
	 */
	public int IsExist2SdFile(String filename) {
		File file = new File(SDCradRoot + filename);
		if (file.exists()) {
			return FileUtils.RESULT_FILE_IS_EXIST;
		}
		return FileUtils.RESULT_FAIL;
	}

	/**
	 * 在sd卡上写入文件流 返回0 写入成功 返回-1写入失败
	 * 
	 * @param path
	 * @param filename
	 * @param inputStream
	 * @return
	 * @throws IOException
	 */
	public int write2SdFromInput(String path, String filename, InputStream inputStream) throws IOException {
		OutputStream outputStream = null;
		String fullpath = getSDCradRoot() + path + filename;
		if (FileUtils.RESULT_SUCCESS == create2SdDir(path)) {
			if (FileUtils.RESULT_SUCCESS == create2SdFile(path + filename)) {
				outputStream = new FileOutputStream(fullpath);
				byte[] buffer = new byte[1024 * 4];
				int temp;
				while ((temp = inputStream.read(buffer)) != -1) {
					outputStream.write(buffer, 0, temp);
				}
				outputStream.flush();
				return FileUtils.RESULT_SUCCESS;
			}
		}
		return FileUtils.RESULT_FAIL;
	}

	/**
	 * 查询本地MP3列表
	 * 
	 * @param path
	 * @return
	 */
	public List<Mp3Info> queryLocalMp3List(String path) {
		List<Mp3Info> infos = new ArrayList<Mp3Info>();
		File file = new File(SDCradRoot + File.separator + path);
		File[] files = file.listFiles();
		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				if (files[i].getName().endsWith("mp3")) {
					Mp3Info mp3Info = new Mp3Info();
					mp3Info.setMp3Name(files[i].getName());
					mp3Info.setMp3Size(files[i].length() + "");
					infos.add(mp3Info);
				}
			}
		}
		return infos;
	}

	/**
	 * 查询本地MP3路径用于播放
	 * 
	 * @param path
	 * @return
	 */
	public String queryLocalMp3Path(String mp3Name) {

		return SDCradRoot + "mp3" + File.separator + mp3Name;
	}

	/**
	 * 查询本地Lrc路径
	 * 
	 * @param path
	 * @return
	 * @throws FileNotFoundException
	 */
	public FileInputStream queryLocalMp3LrcPath(String lrcName) throws FileNotFoundException {

		return new FileInputStream(SDCradRoot + "mp3" + File.separator + lrcName);
	}
}
